package com.htfyun.fragment.library;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.htfyun.CachePngToBmpManager;
import com.htfyun.Constants;
import com.htfyun.fragment.base.BaseWithPageNumberAdapter;
import com.htfyun.utils.DebugLog;
import com.squareup.picasso.Picasso;

import org.geometerplus.zlibrary.ui.android.R;

import java.io.File;
import java.util.List;

/**
 * Created by sst on 2017/12/27.
 */

public class LibraryImageRecyclerViewAdapter extends BaseWithPageNumberAdapter<File, LibraryImageRecyclerViewAdapter.MyViewHolder> {


    public LibraryImageRecyclerViewAdapter(Context context) {
        super(context);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        super.onBindViewHolder(holder, position);


        List<File> items = getItems();

        if (items.size() <= position) {
            return;
        }

        File img = items.get(position);

//        Bitmap bmp = CachePngToBmpManager.INSTANCE.getBitmap(img.getPath());
//        if (bmp == null) {
//
//            int width = getContext().getResources().getDimensionPixelSize(R.dimen.library_img_cover_width);
//            int height = getContext().getResources().getDimensionPixelSize(R.dimen.library_img_cover_height);
//
//            bmp = CachePngToBmpManager.INSTANCE.createBitmapByFilePath(img.getPath(), width, height);
//
//        }
//
//        if (bmp == null) {
//            holder.img.setImageResource(R.drawable.default_error);
//        } else {
//            holder.img.setImageBitmap(bmp);
//        }
        
        Picasso.with(getContext())
                .load(img)
                .error(R.drawable.default_error)
                .resizeDimen(R.dimen.library_img_cover_width, R.dimen.library_img_cover_height)
                .tag(LibraryImageFragment.PICASSO_TAG)
                .into(holder.img);

    }
    
    @Override
    public MyViewHolder getViewHolder(View view) {
        return new MyViewHolder(view);
    }

    @Override
    public int getLayoutId() {
        return R.layout.htfyun_library_image_adapter_item;
    }

    @Override
    public void onItemClick(int position, File file) {

        Intent intent = new Intent("com.htfyun.action.ImgView");
        intent.putExtra(Constants.CURRENT_IMAGE_POSITION, position);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        getContext().startActivity(intent);

    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView img;

        public MyViewHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.img);
        }
    }

}
